<template>
    <div>
        <el-form :inline="true" :model="dynamicSearchParam" class="demo-form-inline">
            <el-form-item>
                <el-input
                    clearable
                    v-model="dynamicSearchParam.content"
                    placeholder="动态内容"
                />
            </el-form-item>
            <el-form-item>
                <el-select
                    clearable
                    v-model="dynamicSearchParam.type"
                    placeholder="类型"
                >
                    <el-option label="悄悄话" value="1"/>
                    <el-option label="提问" value="2"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select
                    clearable
                    v-model="dynamicSearchParam.status"
                    placeholder="请选择状态"
                >
                    <el-option label="待审核" value="1"/>
                    <el-option label="未通过" value="2"/>
                    <el-option label="已通过" value="3"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select
                    clearable
                    v-model="dynamicSearchParam.isHide"
                    placeholder="请选择是否隐藏"
                >
                    <el-option label="隐藏" value="true"/>
                    <el-option label="显示" value="false"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select
                    clearable
                    v-model="dynamicSearchParam.isOpenComment"
                    placeholder="请选择是否开启评论"
                >
                    <el-option label="开启" value="true"/>
                    <el-option label="关闭" value="false"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select
                    clearable
                    v-model="dynamicSearchParam.isSyncLibertySquare"
                    placeholder="是否同步自由广场"
                >
                    <el-option label="同步" value="true"/>
                    <el-option label="未同步" value="false"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select clearable v-model="dynamicSearchParam.publishSchoolUuid" placeholder="请选择学校">
                    <el-option v-for="school in data.schools" :key="school.uuid" :label="school.name"
                               :value="school.uuid"/>
                </el-select>
            </el-form-item>
            <el-form-item style="width: auto">
                <el-button type="primary" @click="search" size="small">查询</el-button>
                <el-button type="primary" @click="exportDynamicData" size="small">导出动态</el-button>
                <el-button type="danger" @click="batchDeleteDynamic" size="small">批量删除</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="data.dynamics" border style="width: 100%" v-loading="data.circleLoading"
                  element-loading-text="拼命加载中" @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" width="70px"/>
            <el-table-column fixed prop="content" label="动态内容" align="center" width="300px"/>
            <el-table-column prop="publishUserNickName" label="发布人" align="center" width="200px"/>
            <el-table-column prop="publishSchoolName" label="发布学校" align="center" width="200px"/>
            <el-table-column prop="circleName" label="发布圈子" align="center" width="200px"/>
            <el-table-column prop="disable" label="是否开启评论" align="center" width="150px">
                <template #default="scope">
                    <el-tag
                        :type="scope.row.isOpenComment ? 'success' : 'danger'"
                        disable-transitions
                    >{{ scope.row.isOpenComment ? "开启" : "关闭" }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="disable" label="是否隐藏" align="center" width="150px">
                <template #default="scope">
                    <el-tag
                        :type="scope.row.isHide ? 'warning' : 'success'"
                        disable-transitions
                    >{{ scope.row.isHide ? "隐藏" : "显示" }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="disable" label="是否同步自由广场" align="center" width="150px">
                <template #default="scope">
                    <el-tag
                        :type="scope.row.isSyncLibertySquare ? 'success' : 'primary'"
                        disable-transitions
                    >{{ scope.row.isSyncLibertySquare ? "同步" : "未同步" }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="type" label="发布类型" align="center" width="150px">
                <template #default="scope">
                    <el-tag
                        type="info"
                        disable-transitions
                        v-if="scope.row.type === 1"
                    >悄悄话
                    </el-tag>
                    <el-tag
                        type="info"
                        disable-transitions
                        v-if="scope.row.type === 2"
                    >提问
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" align="center" width="150px">
                <template #default="scope">
                    <el-tag
                        type="warning"
                        disable-transitions
                        v-if="scope.row.status === 1"
                    >待审核
                    </el-tag>
                    <el-tag
                        type="danger"
                        disable-transitions
                        v-if="scope.row.status === 2"
                    >未通过
                    </el-tag>
                    <el-tag
                        type="success"
                        disable-transitions
                        v-if="scope.row.status === 3"
                    >已通过
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="labels" label="标签" align="center" width="400px">
                <template #default="scope">
                    <el-tag v-for="labelName in scope.row.labelNames" :key="labelName" style="margin: 5px">{{
                            labelName
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="examineCount" label="审核次数" align="center" width="100px"/>
            <el-table-column prop="viewCount" label="浏览数量" align="center" width="100px"/>
            <el-table-column prop="commentCount" label="评论数量" align="center" width="100px"/>
            <el-table-column prop="kudosCount" label="点赞数量" align="center" width="100px"/>
            <el-table-column
                prop="createTime"
                label="创建时间"
                width="180"
                align="center"
            >
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间"
                width="180"
                align="center"
            >
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="300" align="center">
                <template #default="scope">
                    <el-button
                        class="button-space"
                        @click="viewExamine(scope.row.uuid)"
                        v-if="scope.row.status === 1"
                        type="primary"
                        size="small"
                    >审核
                    </el-button>
                    <el-button
                        class="button-space"
                        @click="viewDetail(scope.row.uuid)"
                        type="info"
                        size="small"
                    >详情
                    </el-button>
                    <el-button
                        class="button-space"
                        @click="closeDynamicComment(scope.row.uuid)"
                        v-if="scope.row.isOpenComment"
                        type="danger"
                        size="small"
                    >关闭评论
                    </el-button>
                    <el-button
                        class="button-space"
                        @click="openDynamicComment(scope.row.uuid)"
                        v-if="!scope.row.isOpenComment"
                        type="success"
                        size="small"
                    >开启评论
                    </el-button>

                    <el-popconfirm
                        title="你确定要删除该动态吗？"
                        confirm-button-text="是"
                        cancel-button-text="否"
                        @confirm="deleteDynamic(scope.row.uuid)"
                    >
                        <template #reference>
                            <el-button
                                class="button-space"
                                type="danger"
                                size="small"
                            >删除
                            </el-button>
                        </template>

                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="prev, pager, next"
            :total="data.total"
            style="text-align: center; margin-top: 10px"
            @current-change="currentChangeDynamic"
        >
        </el-pagination>
    </div>
</template>
<script>
import {reactive, onMounted} from "vue";
import {page, openComment, closeComment, deleted, exportData, batchDelete} from "@/api/dynamic/dynamic";
import {all} from "@/api/school/school";
import {useRouter} from "vue-router";
import {ElLoading, ElMessage, ElMessageBox} from "element-plus";
import dayjs from "dayjs";

export default {
    setup() {
        const dynamicSearchParam = reactive({
            page: 1,
            size: 10,
            content: '',
            status: '',
            type: '',
            publishSchoolUuid: '',
            isHide: '',
            isOpenComment: '',
            isSyncLibertySquare: ''
        })
        const data = reactive({
            schools: [],
            dynamics: [],
            total: 0,
            selectUuids: []
        })
        const router = useRouter();
        /**
         * 获取全部学校
         * @returns {Promise<void>}
         */
        const allSchool = async () => {
            all().then(res => {
                data.schools = res.data
            })
        }
        /**
         * 分页查询动态
         */
        const pageSearchDynamic = () => {
            page(dynamicSearchParam).then(res => {
                data.dynamics = res.data.records
                data.total = res.data.total
            })
        }
        /**
         * 搜索查询
         */
        const search = () => {
            dynamicSearchParam.page = 1
            pageSearchDynamic()
        }
        /**
         * 去审核
         * @param uuid
         */
        const viewExamine = (uuid) => {
            router.push({path: "/treeHole/dynamic/examine", query: {uuid: uuid}})
        }
        /**
         * 查看详情
         * @param uuid
         */
        const viewDetail = (uuid) => {
            router.push({path: '/treeHole/dynamic/detail', query: {uuid: uuid}})
        }
        /**
         * 开启动态评论
         * @param uuid
         */
        const openDynamicComment = (uuid) => {
            openComment(uuid).then(() => {
                ElMessage({
                    message: '开启成功',
                    type: 'success',
                })
                pageSearchDynamic()
            })
        }
        /**
         * 开启动态评论
         * @param uuid
         */
        const closeDynamicComment = (uuid) => {
            closeComment(uuid).then(() => {
                ElMessage({
                    message: '关闭成功',
                    type: 'success',
                })
                pageSearchDynamic()
            })
        }
        /**
         * 删除动态
         * @param uuid
         */
        const deleteDynamic = (uuid) => {
            deleted(uuid).then(() => {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                })
                pageSearchDynamic()
            })
        }
        /**
         * 导出动态数据
         */
        const exportDynamicData = () => {
            const loading = ElLoading.service({
                lock: true,
                text: '导出中...',
                background: 'rgba(0, 0, 0, 0.7)',
            })
            exportData(dynamicSearchParam).then(res => {
                const blob = new Blob([res]);
                const day = dayjs().format('YYYY-MM-DD');
                const fileName = `动态列表${day}.xls`;
                const elink = document.createElement('a');
                elink.download = fileName;
                elink.style.display = 'none';
                elink.href = URL.createObjectURL(blob);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
                loading.close()
            })
        }
        /**
         * 点击页数查询动态
         * @param value
         */
        const currentChangeDynamic = (value) => {
            dynamicSearchParam.page = value
            pageSearchDynamic()
        }
        /**
         * 批量选中
         * @param selection
         */
        const handleSelectionChange = (selection) => {
            data.selectUuids = selection.map(select => {
                return select.uuid
            })
        }

        /**
         * 批量删除
         */
        const batchDeleteDynamic = () => {
            if (data.selectUuids.length <= 0) {
                ElMessage({
                    message: '至少选择一个动态',
                    type: 'warning',
                })
                return
            }
            ElMessageBox.confirm(
                '确定要删除所选动态吗?删除后不能恢复',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    batchDelete(data.selectUuids).then(() => {
                        ElMessage({
                            type: 'success',
                            message: '删除成功',
                        })
                        data.selectUuids = []
                        pageSearchDynamic();
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消删除',
                    })
                    data.selectUuids = []
                })
        }
        onMounted(() => {
            pageSearchDynamic()
            allSchool()
        })
        return {
            dynamicSearchParam,
            data,
            search,
            viewExamine,
            viewDetail,
            openDynamicComment,
            closeDynamicComment,
            deleteDynamic,
            exportDynamicData,
            currentChangeDynamic,
            batchDeleteDynamic,
            handleSelectionChange
        }
    }
}
</script>